package com.rear.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rear.entity.MessageEntity;
import com.rear.entity.MessageTypeEntity;
import com.rear.service.MessageService;
import com.rear.service.MessageTypeService;
import com.rear.service.SysUserService;
import com.rear.utils.DateUtil;
import com.rear.utils.PageUtils;
import com.rear.utils.R;


/**
 * 
 * 
 * @author October
 * @email chenzeming@9an-data.com
 * @date 2017-04-14 12:00:02
 */
@Controller
@RequestMapping("message")
public class MessageController {
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private MessageTypeService messageTypeService;
	
	@Autowired
	private SysUserService sysUserService;
	
	@RequestMapping("/message.html")
	public String list(){
		return "message/message.html";
	}
	@RequestMapping("/messageShow.html")
	public String show(){
		return "message/messageShow.html";
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list/new")
	@RequiresPermissions("message:list")
	public R listNew(
			//消息类型
			String messageType,
			//消息标题
			String messageHeader,
			//消息状态
			String status,
			//消息来源
			String messageSource,
			//展示时间
			String displayTime,
			//过期时间
			String timeExpired,
			
			String qstatus,
			Integer page, 
			Integer limit,
			HttpServletRequest req,
			HttpServletResponse resp){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		
		//消息类型
		map.put("messageType",messageType);
		//消息标题
		map.put("messageHeader",messageHeader);
		//消息状态
		map.put("status",status);
		//消息来源
		map.put("messageSource",messageSource);
		//展示时间
		map.put("displayTime",DateUtil.getSqlDate(DateUtil.getDate(DateUtil.yyyyMMddToStartDataLong(displayTime)),"yyyy-MM-dd"));
		//过期时间
		map.put("timeExpired",DateUtil.getSqlDate(DateUtil.getDate(DateUtil.yyyyMMddToEndDataLong(timeExpired)),"yyyy-MM-dd"));
		
		//渠道标识
		String username =req.getSession().getAttribute("username").toString();
		map.put("channelid", sysUserService.getappid(username));
		//查询页面
		map.put("qstatus", "new");
		//查询列表数据
		List<MessageEntity> messageList = messageService.queryList(map);
		int total = messageService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(messageList, total, limit, page);
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list/pendingAudit")
	@RequiresPermissions("message:list")
	public R listPendingAudit(
			//消息类型
			String messageType,
			//消息标题
			String messageHeader,
			//消息状态
			String status,
			//消息来源
			String messageSource,
			//展示时间
			String displayTime,
			//过期时间
			String timeExpired,
			
			String qstatus,
			Integer page, 
			Integer limit,
			HttpServletRequest req,
			HttpServletResponse resp){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		
		//消息类型
		map.put("messageType",messageType);
		//消息标题
		map.put("messageHeader",messageHeader);
		//消息状态
		map.put("status",status);
		//消息来源
		map.put("messageSource",messageSource);
		//展示时间
		map.put("displayTime",DateUtil.getSqlDate(DateUtil.getDate(DateUtil.yyyyMMddToStartDataLong(displayTime)),"yyyy-MM-dd"));
		//过期时间
		map.put("timeExpired",DateUtil.getSqlDate(DateUtil.getDate(DateUtil.yyyyMMddToEndDataLong(timeExpired)),"yyyy-MM-dd"));
		
		//渠道标识
		String username =req.getSession().getAttribute("username").toString();
		map.put("channelid", sysUserService.getappid(username));
		//查询页面
		map.put("qstatus", "pendingAudit");
		//查询列表数据
		List<MessageEntity> messageList = messageService.queryList(map);
		int total = messageService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(messageList, total, limit, page);
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list/auditThrough")
	@RequiresPermissions("message:list")
	public R listAuditThrough(
			//消息类型
			String messageType,
			//消息标题
			String messageHeader,
			//消息状态
			String status,
			//消息来源
			String messageSource,
			//展示时间
			String displayTime,
			//过期时间
			String timeExpired,
			
			String qstatus,
			Integer page, 
			Integer limit,
			HttpServletRequest req,
			HttpServletResponse resp){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		
		//消息类型
		map.put("messageType",messageType);
		//消息标题
		map.put("messageHeader",messageHeader);
		//消息状态
		map.put("status",status);
		//消息来源
		map.put("messageSource",messageSource);
		//展示时间
		map.put("displayTime",DateUtil.getSqlDate(DateUtil.getDate(DateUtil.yyyyMMddToStartDataLong(displayTime)),"yyyy-MM-dd"));
		//过期时间
		map.put("timeExpired",DateUtil.getSqlDate(DateUtil.getDate(DateUtil.yyyyMMddToEndDataLong(timeExpired)),"yyyy-MM-dd"));
		
		//渠道标识
		String username =req.getSession().getAttribute("username").toString();
		map.put("channelid", sysUserService.getappid(username));
		//查询页面
		map.put("qstatus", "auditThrough");
		//查询列表数据
		List<MessageEntity> messageList = messageService.queryList(map);
		int total = messageService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(messageList, total, limit, page);
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("message:info")
	public R info(@PathVariable("id") Integer id){
		MessageEntity message = messageService.queryObject(id);
		
		return R.ok().put("message", message);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("message:save")
	public R save(@RequestBody MessageEntity message,
			HttpServletRequest req,
			HttpServletResponse resp){
		String username =req.getSession().getAttribute("username").toString();
		//渠道标识
		message.setChannelid(sysUserService.getappid(username));
		message.setCreateuser(username);
		message.setMessageSource(MessageEntity.MESSAGE_SOURCE_SELF);
		message.setStatus(MessageEntity.NEW);
		messageService.save(message);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("message:update")
	public R update(@RequestBody MessageEntity message,
			HttpServletRequest req,
			HttpServletResponse resp){
		String username =req.getSession().getAttribute("username").toString();
		message.setUpdateuser(username);
		messageService.update(message);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("message:delete")
	public R delete(@RequestBody Integer[] ids){
		messageService.deleteBatch(ids);
		
		return R.ok();
	}
	
	
	
	/**
	 * 查询操作主题列表数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/select")
	@RequiresPermissions("message:list")
	public R select(
			HttpServletRequest req,
			HttpServletResponse resp){
		Map<String, Object> map = new HashMap<>();
		//渠道标识
		String username =req.getSession().getAttribute("username").toString();
		map.put("channelid", sysUserService.getappid(username));
		//查询列表数据
		List<MessageTypeEntity> messageTypeList = messageTypeService.typeList(map);

		
		return R.ok().put("list", messageTypeList);
	}
	/**
	 * 提交审核
	 */
	@ResponseBody
	@RequestMapping("/pendingAudit")
	@RequiresPermissions("message:pendingAudit")
	public R pendingAudit(@RequestBody MessageEntity message){
		Integer id=message.getId();
		MessageEntity messageOld = messageService.queryObject(id);
		if(MessageEntity.NEW.equals(messageOld.getStatus())||MessageEntity.INVALID.equals(messageOld.getStatus())){
			message.setId(id);
			message.setStatus(MessageEntity.PENDING_AUDIT);
			messageService.update(message);
			return R.ok();
		}else{
			return R.error("该消息已经审核或正在审核中，请勿重复提交！");
		}
	}
	/**
	 * 审核通过
	 */
	@ResponseBody
	@RequestMapping("/auditThrough")
	@RequiresPermissions("message:auditThrough")
	public R auditThrough(@RequestBody MessageEntity message){
		Integer id=message.getId();
		MessageEntity messageEntity=new MessageEntity();
		messageEntity.setId(id);
		messageEntity.setStatus(MessageEntity.AUDIT_THROUGH);
		messageService.update(messageEntity);
		return R.ok();
	}
	/**
	 * 已发布 不是审核通过的不能发布
	 */
	@ResponseBody
	@RequestMapping("/alreadyReleased")
	@RequiresPermissions("message:alreadyReleased")
	public R alreadyReleased(@RequestBody MessageEntity message,
			HttpServletRequest req){
		Integer id=message.getId();
		MessageEntity messageOld = messageService.queryObject(id);
		if(MessageEntity.AUDIT_THROUGH.equals(messageOld.getStatus())){
			MessageEntity messageEntity=new MessageEntity();
			messageEntity.setId(id);
			messageEntity.setStatus(MessageEntity.ALREADY_RELEASED);
			String username =req.getSession().getAttribute("username").toString();
			messageEntity.setPublishuser(username);
			messageEntity.setPublishtime(new Date());
			messageService.update(messageEntity);
			return R.ok();
		}else{
			return R.error("该消息未经审核不能发布！");
		}
	}
	/**
	 * 消息失效
	 */
	@ResponseBody
	@RequestMapping("/invalid")
	@RequiresPermissions("message:invalid")
	public R invalid(@RequestBody MessageEntity message){
		Integer id=message.getId();
		MessageEntity messageEntity=new MessageEntity();
		messageEntity.setId(id);
		messageEntity.setStatus(MessageEntity.INVALID);
		messageService.update(messageEntity);
		return R.ok();
	}
}
